package com.tool.cn.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 * 
 * @author wangzh
 * 2018年1月2日 下午3:26:36
 */
public final class DateUtil {

	/**
	 * 秒级时间戳转时间 yyyy-MM-dd HH:mm:ss
	 */
	public static String UTCTodate(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		int zero = 13 - time.length();
		for (int i = 0; i < zero; i++) {
			time += "0";
		}
		return sdf.format(new Date(Long.parseLong(time)));
	}
	
	/**
	 * 时间转秒级时间戳 yyyy-MM-dd HH:mm:ss
	 * @param datetime
	 * @return
	 */
	public static String dateToUTC(String datetime){
		java.util.Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String utc = String.valueOf(date.getTime()/1000);
		return utc;
	}
	
	/**
	 * 获取当前秒级UTC时间
	 * @return
	 */
	public static Long dateToUTC(){
		Date date=new Date();
		return date.getTime()/1000;
	}
	
	/**
	 * 毫秒时间戳转时间
	 */
	public static String getDataDetail(String time) {
		String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(time);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
	}


	/**
	 * 时间转换毫秒时间戳
	 */
	public static String formatStrToDayFirstSec(String time) {
		 String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=null;
		try {
			date = simpleDateFormat.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
	}
	

	/**
	 * 获取时间
	 * @param time
	 * @return
	 */
	public static long formatStr(String time) {
		long d = 0;
		String f = "yyyyMMddHHmmss";
		switch (time.length()) {
		case 4:
			f = "yyyy";
			break;
		case 6:
			f = "yyyyMM";
			break;
		case 8:
			f = "yyyyMMdd";
			break;
		case 10:
			f = "yyyyMMddHH";
			break;
		case 12:
			f = "yyyyMMddHHmm";
			break;
		default:
			f = "yyyyMMddHHmmss";
		}
		SimpleDateFormat fmt = new SimpleDateFormat(f);
		try {
			Date dt = fmt.parse(time);
			d = dt.getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 获取当前时间
	 */
	public static String getTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // yyyy-mm-dd
																	// hh:mm:ss
		return sdf.format(new Date());
	}
	
	public static String getTime(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		return sdf.format(date); 
	}

	/**
	 * 得到几天前的时间
	 * day 正数 加几天  负数 减几天
	 */
	public static Date getDateBefore(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE)+day);
		return now.getTime();
	}

	/**
	 * 得到年
	 */
	public static Date getYearBefore(Date date, int year) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.YEAR, year);
		return now.getTime();
	}

	/**
	 * 得到月
	 */
	public static Date getMonthBefore(Date date, int month) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.MONTH, month);
		return now.getTime();
	}
	
	/**
	 * 获取月第一天
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date){
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
	        cal.set(Calendar.DAY_OF_MONTH, firstDay);
	        return cal.getTime();
	}
	
	/**
	 * 获取月最后一天
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date){
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	        cal.set(Calendar.DAY_OF_MONTH, lastDay);
	        return cal.getTime();
	}
	
	/**
	 * 判断是不是周六日
	 */
	public static boolean isWeek(Date date) {
		if(isSaturday(date)||isSunday(date)){
			return true;
		}
		return false;
		
	}
	
	/**
	 * 判断周六
	 */
	public static boolean isSaturday(Date date){
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){  
		    return true;
		}
		return false;
	}
	
	/**
	 * 判断周末
	 */
	public static boolean isSunday(Date date){
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){  
		    return true;  
		}  
		return false;
	}
	
	/**
	 * 获取当前时间周一到周五日期
	 */
	public static List <String> getSearchTime(){
		  List<String> searchTime=new ArrayList<String>();
		  Date date=new Date();
		  while(true){
			  if(isWeek(date)){
				  date=getDateBefore(date, -1);
			  }else{
				  searchTime.add(getTime(date));
				  date=getDateBefore(date, -1);
			  }
			  
			  if(searchTime.size()==5){
				  break;
			  }
		  }
		  
		  return searchTime;
	}
	
	/**
	 * 效验时间是否为制定格式
	 */
	public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

}
